package bio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * @Author: admin
 * @Description:
 * @Date: 2019/6/12 11:25
 */

public class NioServer {
    private int port;
    private Selector bossSelector;
    private Selector workSelector;
    private ServerSocketChannel serverSocketChannel;

    public NioServer(int port) {
        this.port = port;
    }

    public void start() throws IOException {

        bossSelector = Selector.open();
        workSelector = Selector.open();

        new Thread(() -> {
            try {
                serverSocketChannel = ServerSocketChannel.open();
                serverSocketChannel.configureBlocking(false);
                serverSocketChannel.bind(new InetSocketAddress(port));
                //serverSocketChannel.socket().bind(new InetSocketAddress(port));
                serverSocketChannel.register(bossSelector, SelectionKey.OP_ACCEPT);
                while (true) {
                    if (bossSelector.select(1) > 0) {
                        Set<SelectionKey> keys = bossSelector.selectedKeys();
                        Iterator<SelectionKey> keyIterator = keys.iterator();

                        while (keyIterator.hasNext()) {
                            SelectionKey key = keyIterator.next();
                            if (key.isAcceptable()) {
                                ServerSocketChannel acceptServerSocketChannel = (ServerSocketChannel) key.channel();
                                SocketChannel clientSocketChannel = acceptServerSocketChannel.accept();
                                clientSocketChannel.configureBlocking(false);
                                clientSocketChannel.register(workSelector, SelectionKey.OP_READ);
                                System.out.println("isAcceptable");
                            }
                            keyIterator.remove();
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();


        new Thread(() -> {
            try {
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                byte[] temp = new byte[1024];
                while (true) {
                    if (workSelector.select(1) > 0) {
                        Set<SelectionKey> keys = workSelector.selectedKeys();
                        Iterator<SelectionKey> keyIterator = keys.iterator();
                        while (keyIterator.hasNext()) {
                            SelectionKey key = keyIterator.next();
                            if (key.isReadable()) {
                                buffer.clear();
                                SocketChannel clientSocketChannel = (SocketChannel) key.channel();
                                clientSocketChannel.configureBlocking(false);
                                int count = 0;
                                StringBuilder sb = new StringBuilder();
                                while ((count = clientSocketChannel.read(buffer)) > 0) {
                                    buffer.flip();
                                    buffer.get(temp, 0, count);
                                    sb.append(new String(temp, 0, count));
                                    buffer.clear();
                                }
                                System.out.println(sb.toString());
                            }
                            keyIterator.remove();
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();


    }

    public static void main(String[] args) throws IOException {
        new NioServer(9999).start();
    }
}
